<template>
	<view class="container">
		<rf-navbar title="登录" @click="onBack"></rf-navbar>
		<view style="margin-top: 200px; display: flex; flex-direction: column; align-items: center;">
			<view class="icon">
				<image class="icon-logo" src="../../static/images/common/icon.png" style="width: 70px; height: 70px;">
				</image>
			</view>
			<text style="display: block; margin-top: 14px;font-size: 16px;color: #273344;">超级小助教</text>
		</view>
		<view style="width: 100%; margin-top: 64px;">

			<button class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" plain="true"
				v-if="YesorNo=='yes'">手机号一键登录</button>
			<view class="btn" @click="submit" v-else>手机号一键登录</view>
			<view style=" margin: 10px 34px;font-size: 24rpx;">

				<u-radio-group v-model="YesorNo" placement="column">
					<u-radio :customStyle="{marginBottom: '20rpx'}" name="yes" shape="circle" activeColor="#58C866"
						inactiveColor="#E3E6E9" size="16">
						<view>
							<text style="color: #3A3D4B;">我已阅读并同意</text>
							<text style="color: #58C866;" @click="onClickXy">《超级小助教用户协议》</text>
							<text style="color: #58C866;" @click="onClickYs">《隐私政策》</text>
						</view>
					</u-radio>
				</u-radio-group>
			</view>
		</view>
		<rf-load :loading="loading" title="登录中..."></rf-load>

	</view>
</template>

<script>
	import {
		mapMutations,
		mapState,
	} from 'vuex'; //引入mapState

	import {
		getcode,
		getOpenid,
		toLogin,
	} from "@/api/login.js"

	export default {
		data() {
			return {
				YesorNo: "",

				loading: false,
			}
		},
		methods: {
			...mapMutations(["login", "updateState"]),
			onBack() {
				uni.navigateBack();
			},
			onClickXy() {
				console.log(this.YesorNo);
				uni.navigateTo({
					url: "/pages-sub/set/agreement"
				})
			},
			onClickYs() {
				uni.navigateTo({
					url: "/pages-sub/set/privacy"
				})
			},
			submit() {
				if (this.YesorNo != "yes") {
					uni.showToast({
						title: "请阅读用户协议与隐私政策",
						icon: 'none',
						duration: 2000
					})
					return
				}
			},
			async _login(e, code) {
				if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
					console.log('用户拒绝提供手机号');
				} else {
					this.loading = true
					console.log('用户同意提供手机号');

					console.log(e)
					let res = await getOpenid(code)

					let encryptedData = e.detail.encryptedData;
					let iv = e.detail.iv;
					let inviter = uni.getStorageSync("inviter_uuid")
					console.log("ssssss");
					toLogin(res.data, iv, encryptedData, inviter).then(res => {
						this.loading = false
						console.log("ppppppp");
						console.log(res);
						// uni.setStorageSync("token", res.token)
						// uni.setStorageSync("userInfo", res.userInfo)
						this.updateState(res)
						// 方法二
						// this.$store.commit("updateState", {
						// 	hasLogin: true,
						// 	userInfo: res.userInfo,
						// });
						uni.navigateBack()
					}).catch(err => {
						this.loading = false
						console.log(err);
					})
				}
			},
			async getPhoneNumber(e) {

				let code = await getcode()
				var _that = this
				setTimeout(() => {
					_that._login(e, code)
				}, 1000)

			}

		}

	}
</script>

<style lang="scss">
	page {}

	.container {
		display: flex;
		flex-direction: column;
		align-items: center;

		.icon {
			width: 90px;
			height: 90px;
			background: #FFFFFF;
			border-radius: 20px;
			border: 1px solid #EAEEF2;

			display: flex;
			justify-content: center;
			align-items: center;
		}

		.btn {
			margin: 0 auto 28px;
			width: 94%;
			height: 44px;
			background: #58C866;
			border-radius: 22px;
			font-size: 32rpx;
			font-weight: 600;
			color: #ffffff;
			line-height: 44px;
			text-align: center;
			border: none;
		}
	}
</style>